<?php
//téléchargement des packages
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['packages_versions']) || count($_SESSION['install']['packages_versions'])==0)
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
if(!$error)
{
	$temp = $project_path.'/temp/';
	fwTools::mkdirTree($project_path, 'temp');
	$total_size = 0;
	foreach($_SESSION['install']['packages_versions'] as $package)
	{
		$total_size+=$package['size'];
	}
	$down_size = 0;
	$next_step = true;
	$packages = $_SESSION['install']['packages_versions'];
	session_write_close();
	foreach($packages as $index => $package)
	{
		session_start();
		if($_SESSION['install']['cancel'])
		{
			$next_step = false;
		}
		session_write_close();
	
		$file = $package['name'].'.'.str_replace('.', '_', $package['version']).'.update.tgz';
		if(file_exists($temp.$file))
		{
			if(filesize($temp.$file)==$package['size'] && md5_file($temp.$file)==$package['md5'])
			{
				$down_size+=$package['size'];
				continue;
			}
			elseif(filesize($temp.$file)>$package['size'] || (filesize($temp.$file)==$package['size'] && md5_file($temp.$file)!=$package['md5']))
			{
				unlink($temp.$file);
			}
		}
		
		if(is_file($temp.$file))
		{
			$start = filesize($temp.$file);
			$file_size =$start;
			$down_size+=$start;
			$w = fopen($temp.$file, 'a+');
		}
		else 
		{
			$start = 0;
			$file_size = 0;
			$w = fopen($temp.$file, 'w');
		}
		$f = fopen($package['host'].'index.php?action=downlad_version&mode=update&application='.$package['name'].'&version='.$package['version'].'&start='.$start, 'r');
		if(!$f)
		{
			echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
			$error = true;
			break;
		}
		$last_progree= microtime(true);
		while(microtime(true)-$start_time<24 && !feof($f))
		{
			$str = fread($f, 4098);
			fwrite($w, $str);
			$down_size+=strlen($str);
			$file_size+=strlen($str);
			if((microtime(true)-$last_progree)*1000>450)
			{
				session_start();
				$_SESSION['install']['progress'] = round(($down_size*1000)/$total_size);
				$_SESSION['install']['status'] = '«!ADMIN_INSTALL_DOWNLOAD_STATUS» '.$package['version'].' ['.($index+1).'/'.count($_SESSION['install']['packages_versions']).']';
				if($_SESSION['install']['cancel'])
				{
					die();
				}
				session_write_close();
				$last_progree= microtime(true);
			}
		}
		$ended = (feof($f) || $file_size>$package['size']);
		fclose($f);
		fclose($w);

		if($ended)
		{
			if($file_size!=$package['size'] || md5_file($temp.$file)!=$package['md5'])
			{
				unlink($temp.$file);
				$next_step = false;
				if(!isset($_SESSION['install']['counter'][$file]))
				{
					$_SESSION['install']['counter'][$file] = 0;
				}
				else
				{
					$_SESSION['install']['counter'][$file]++;
					if($_SESSION['install']['counter'][$file]>5)
					{
						echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADINGFORMAT»");';
						$error = true;
					}
				}
			}
			else
			{
				echo 'updateUpdateStepCurrent("'.$package['name'].'", "'.$package['version'].'", null);
updateUpdateStepDone("'.$package['name'].'", "'.$package['version'].'", 1);';
				$next_step = true;
			}
		}
		else
		{
			$next_step = false;
			break;
		}
	}
}
if(!$error)
{
	if($next_step)
	{
		session_start();
		$_SESSION['install']['screen'] = 2;
		$_SESSION['install']['progress'] = 0;
		$_SESSION['install']['status'] = '';
		echo 'updateStopProgress();
updateUpdateStepCurrent(null, null, null);
updateUpdateStepDone(null, null, 1);
updateUpdateStepCurrent("'.$_SESSION['install']['packages_versions'][0]['name'].'", "'.$_SESSION['install']['packages_versions'][0]['version'].'", 2)
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_CHECK_HEAD»", "«!ADMIN_INSTALL_CHECK_STATUS»");';
	}
	else
	{
		echo 'updatePerform("'.$_SESSION['install']['id'].'");';
	}
}
?>